package com.yytek.nmp.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.yytek.common.client.base.MqMessage;
import com.yytek.common.core.constant.CacheConstants;
import com.yytek.common.core.domain.DeviceCache;
import com.yytek.common.core.utils.DateUtils;
import com.yytek.common.web.domain.ProCommonMessage;
import com.yytek.common.web.domain.ThingModelMessage;
import com.yytek.nmp.dto.DeviceStateCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yytek.nmp.mapper.IotEventHistoryMapper;
import com.yytek.nmp.domain.IotEventHistory;
import com.yytek.nmp.service.IIotEventHistoryService;

import javax.annotation.Resource;

/**
 * 事件历史Service业务层处理
 * 
 * @author yytek-iot
 * @date 2022-07-13
 */
@Service
public class IotEventHistoryServiceImpl implements IIotEventHistoryService 
{
    @Autowired
    private IotEventHistoryMapper iotEventHistoryMapper;
    @Resource
    private IotDeviceInfoServiceImpl iotDeviceInfoService;
    /**
     * 查询事件历史
     * 
     * @param id 事件历史主键
     * @return 事件历史
     */
    @Override
    public IotEventHistory selectIotEventHistoryById(Long id)
    {
        return iotEventHistoryMapper.selectIotEventHistoryById(id);
    }

    /**
     * 查询事件历史列表
     * 
     * @param iotEventHistory 事件历史
     * @return 事件历史
     */
    @Override
    public List<IotEventHistory> selectIotEventHistoryList(IotEventHistory iotEventHistory)
    {
        return iotEventHistoryMapper.selectIotEventHistoryList(iotEventHistory);
    }

    /**
     * 新增事件历史
     * 
     * @param iotEventHistory 事件历史
     * @return 结果
     */
    @Override
    public int insertIotEventHistory(IotEventHistory iotEventHistory)
    {
        iotEventHistory.setCreateTime(DateUtils.getNowDate());
        return iotEventHistoryMapper.insertIotEventHistory(iotEventHistory);
    }

    /**
     * 修改事件历史
     * 
     * @param iotEventHistory 事件历史
     * @return 结果
     */
    @Override
    public int updateIotEventHistory(IotEventHistory iotEventHistory)
    {
        iotEventHistory.setUpdateTime(DateUtils.getNowDate());
        return iotEventHistoryMapper.updateIotEventHistory(iotEventHistory);
    }

    /**
     * 批量删除事件历史
     * 
     * @param ids 需要删除的事件历史主键
     * @return 结果
     */
    @Override
    public int deleteIotEventHistoryByIds(Long[] ids)
    {
        return iotEventHistoryMapper.deleteIotEventHistoryByIds(ids);
    }

    /**
     * 删除事件历史信息
     * 
     * @param id 事件历史主键
     * @return 结果
     */
    @Override
    public int deleteIotEventHistoryById(Long id)
    {
        return iotEventHistoryMapper.deleteIotEventHistoryById(id);
    }

    /**
     * 处理设备事件上传消息
     * @param mqMsg
     */
    public void sendEventPostByMsg(MqMessage mqMsg) {
        String msg = new String(mqMsg.getPayload());
        //解析line中换成接收的参数实体
        ThingModelMessage thingModelMessage= JSON.parseObject
                (msg,ThingModelMessage.class);
        if(CollectionUtil.isNotEmpty(thingModelMessage.getData())){
            String[] a= thingModelMessage.getIdentifier().split("\\.");
            //事件名称
            String identifier = a[2];
            //设备事件上传
            List<ProCommonMessage> list = thingModelMessage.getData();
            //循环设备事件参数
            for (int i = 0; i < list.size(); i++) {
                ProCommonMessage commonMessage = list.get(i);
                //设备id
                String iotId = commonMessage.getDeviceId();
                //上报时间
                Long dcTime = commonMessage.getDctime();
                if(dcTime == null){
                    dcTime = DateUtil.current();
                }
                DeviceCache deviceCache = iotDeviceInfoService.getDeviceCacheByIotId(iotId);
                //产品id
                Integer productId = deviceCache.getProductId();
                //上报内容
                String content = null;
                if(commonMessage.getParams() != null){
                    content = JSON.toJSONString(commonMessage.getParams());
                }
                //设备事件记录
                IotEventHistory iotEventHistory = new IotEventHistory();
                iotEventHistory.setProductId(Long.valueOf(productId));
                iotEventHistory.setDeviceId(iotId);
                iotEventHistory.setDeviceName(deviceCache.getIotId());
                iotEventHistory.setEventName(identifier);
                iotEventHistory.setContent(content);
                iotEventHistory.setReportTime(new Date(dcTime));
                iotEventHistory.setCreateBy("sys");
                iotEventHistory.setCreateTime(new Date());
                iotEventHistoryMapper.insertIotEventHistory(iotEventHistory);
            }

        }
    }
}
